package com.lx.patterns.singleton.demo5;


import java.io.*;
import java.lang.reflect.Constructor;

/**
 * @ClassName Client
 * @Description: 测试
 * @Author: 落雪
 * @CreateDate: 2025/1/12 16:58
 * @UpdateUser: 落雪
 * @UpdateDate: 2025/1/12 16:58
 * @UpdateRemark:
 * @Version: 1.0
 */
public class Client {

    public static void main(String[] args) throws Exception {
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1==instance2);
        writeObject();
        readObject();
        readObject();


        Class clazz = Singleton.class;

        Constructor declaredConstructor = clazz.getDeclaredConstructor();
        declaredConstructor.setAccessible(true);
        Object o1 = declaredConstructor.newInstance();
        Object o2 = declaredConstructor.newInstance();
        System.out.println(o1==o2);

    }


    public static void readObject() throws Exception{

        ObjectInputStream ins = new ObjectInputStream(new FileInputStream("/Users/luoxue/test/a.txt"));

        Object o = ins.readObject();

        System.out.println(o);

    }

    public static void writeObject() throws Exception{

        Singleton instance = Singleton.getInstance();
        ObjectOutputStream objectInputStream = new ObjectOutputStream(new FileOutputStream("/Users/luoxue/test/a.txt"));
        objectInputStream.writeObject(instance);

    }
}
